<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索添加区域 -->
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="userList" border stripe>
        <el-table-column label="序" type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="uemail"></el-table-column>
        <el-table-column label="电话" prop="uphone"></el-table-column>
        <el-table-column label="角色" prop="urole"></el-table-column>
        <el-table-column label="状态">
          <!-- 作用域插槽 -->
          <template v-slot:default="userList">
            <el-switch
              v-model="userList.row.ustatus"
              active-value="1"
              inactive-value="0"
              @change="userStatusChanged(userList.row.ustatus)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot:default="userList">
            <el-button
              @click="showEditDialog(userList.row.uid)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removerUserById(userList.row.uid)"
            ></el-button>
            <el-tooltip effect="dark" content="分配全选" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      ></el-pagination>
    </el-card>

    <!-- 添加的开关对话框 -->
    <el-dialog @close="addDialogClose" title="提示" :visible.sync="addVisible" width="50%">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
        <!-- 添加主体区 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="uemail">
          <el-input v-model="addForm.uemail"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="uphone">
          <el-input v-model="addForm.uphone"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="EditVisible" width="50%" @close="EditDialogClose">
      <el-form :model="EditForm" :rules="addFormRules" ref="EditFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="EditForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="EditForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="uemail">
          <el-input v-model="EditForm.uemail"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="uphone">
          <el-input v-model="EditForm.uphone"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditVisible = false">取 消</el-button>
        <el-button type="primary" @click="EditUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 自定义校验
    var checkphone = (rule, value, cb) => {
      const regs = /^((13[0-9])|(17[0-1,6-8])|(15[^4,\\D])|(18[0-9]))\d{8}$/
      if (regs.test(value)) {
        return cb()
      } else {
        cb(new Error('请输入合法的手机号'))
      }
    }
    return {
      // 获取用户列表的阐述
      queryInfo: {
        query: '',
        pageing: 1,
        pages: 5
      },
      userList: [],
      // 对话框开关属性
      addVisible: false,
      EditVisible: false,
      //添加用户的数据
      addForm: {
        username: '',
        password: '',
        uemail: '',
        uphone: ''
      },
      EditForm: {},
      page: {},
      // 表单验证
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名的长度在 3 ~ 10 之间',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '密码的长度在 3 ~ 10 之间',
            trigger: 'blur'
          }
        ],
        uemail: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        uphone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkphone,
            message: '请输入合法的手机号',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get('/user/userList', {
        params: this.queryInfo
      })
      this.userList = res.data.rows
      this.page = res.data
      if (!res.flag) {
        return this.$message.error('获取用户列表失败')
      }
      console.log(this.userList)
    },
    // 监听开关的状态
    userStatusChanged(ustatus) {
      this.$http.put('/user/')
    },
    // 监听对话框的关闭
    addDialogClose() {
      this.$refs.addFormRef.resetFields()
    },
    EditDialogClose() {
      this.$refs.EditFormRef.resetFields()
    },
    addUser() {
      this.$refs.addFormRef.validate(async valide => {
        if (!valide) {
          return
        } else {
          const { data: res } = await this.$http.post(
            'user/adduser',
            this.addForm
          )
          if (!res.flag) {
            this.$message.error('添加用户失败')
          } else {
            this.$message.success('添加用户成功')
          }
          // 隐藏添加用户的对话框
          this.addVisible = false
          this.getUserList()
        }
      })
    },
    // 监控page size改变
    handleSizeChange(newSize) {
      this.queryInfo.pages = newSize
      this.getUserList()
    },
    // 监控页面值改变事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageing = newPage
      this.getUserList()
    },
    // 展示修改对话框
    async showEditDialog(uid) {
      this.EditVisible = true
      const { data: res } = await this.$http.get('user/findUserById/' + uid)
      if (!res.flag) {
        return this.$messages.error('查询用户信息失败')
      } else {
        this.EditForm = res.data
      }
    },
    async EditUser() {
      const { data: res } = await this.$http.put(
        '/user/userUpdate',
        this.EditForm
      )
      if (!res.flag) {
        return this.$message.error('修改失败')
      }

      //关闭对话框
      this.EditVisible = false
      // 刷新列表
      this.getUserList()
      return this.$message.success('更新成功')
    },
    async removerUserById(uid) {
      const a = await this.$confirm('此操作将永久删除该用户,是否继续', '提示', {
        type: 'warning'
      }).catch(err => err)   
      
      
     
      if (a !== 'confirm') {
        return this.$message.success('操作已取消')
      }
      const { data: res } = await this.$http.delete(
        '/user/removerUserById/' + uid
      )
      if (!res.flag) {
         return this.$message.error('删除用户失败')
      }else{
        this.getUserList()
        return this.$message.success('删除用户成功')
        
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>